﻿Public Class Slider
    Private M_TopBorder As Integer = 15
    Private M_BottomBorder As Integer = 15
    Private M_LeftBorder As Integer = 10
    Private M_RightBorder As Integer = 10

    Private M_TrackColor As Color = Color.Black

    Private M_KnobWidth As Integer = 40
    Private M_Knobheight As Integer = 12
    Private M_KnobColor As Color = Color.Blue
    Private M_ShowKnobExtenderLine As Boolean = True
    Private M_ExtensionLineColor As Color = Color.Red
    Private M_ExtensionLineWidth As Integer = 2

    Private M_BackgroundType As BackGroundTypeEnum = BackGroundTypeEnum.Raised3D
    Private m_BackGroundColor As Color = Color.Silver

    Private MouseVal As Integer
    Private BackImage As Bitmap

    Private Track_Max As Integer
    Private Track_Len As Integer
    Private Track_Min As Integer
    Private Master_Span As Integer
    Private Center As Integer
    Private BackPic As New CreateBackground
    Private Knob As Bitmap
    Private BackgroundPic As Bitmap
    Private UpdateBackpic As Boolean = False

    Public Enum BackGroundTypeEnum
        None = 0
        Raised3D = 1
    End Enum
    Private Sub UserControl1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            MouseVal = e.Y
            If MouseVal < Track_Max Then MouseVal = Track_Max
            If MouseVal > Track_Min Then MouseVal = Track_Min
            Refresh()
        End If
    End Sub

    Private Sub Slider_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'If UpdateBackpic Then
        'e.Graphics.DrawImage(BackgroundPic, 0, 0)
        'UpdateBackpic = False
        'End If
        DrawKnob(e.Graphics)
    End Sub
    Private Sub UserControl1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        UpdateVars()
        DrawBackGround()
        Refresh()
    End Sub
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.BackColor = Color.Transparent
        Me.UpdateStyles()

        Knob = BackPic.CreateScaledPic(M_KnobWidth, M_Knobheight, M_KnobColor, 0.5)
        UpdateVars()
        DrawBackGround()
    End Sub

    'Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    'End Sub
    Private Sub UpdateVars()
        Center = Me.Width / 2
        Track_Max = M_TopBorder
        Track_Min = Me.Height - M_BottomBorder
        Track_Len = Track_Min - Track_Max
    End Sub
    Private Sub DrawBackGround()
        BackgroundPic = New Bitmap(Me.Width, Me.Height, Imaging.PixelFormat.Format32bppArgb)
        Dim G As Graphics = Graphics.FromImage(BackgroundPic)
        G.Clear(Me.BackColor)
        Dim ClearBrush As New SolidBrush(Me.BackColor)
        If M_BackgroundType = BackGroundTypeEnum.Raised3D Then
            BackgroundPic = BackPic.CreatePic(Me.Width, Me.Height, m_BackGroundColor)
            ClearBrush = New SolidBrush(BackPic.FaceColor)
        End If
        Dim ClearPen As New Pen(Me.BackColor)
        'Draw Scale
        Master_Span = Track_Len
        Dim PenMinor As New Pen(Color.Black, 1)
        Dim PenMajor As New Pen(Color.Black, 2)
        Dim Space As Integer = Master_Span / 20
        For c = Track_Max To Track_Min Step Space
            G.DrawLine(PenMinor, M_LeftBorder, c, Me.Width - M_RightBorder, c)
        Next
        Space = Master_Span / 10
        For c = Track_Max To Track_Min Step Space
            G.DrawLine(PenMajor, M_LeftBorder, c, Me.Width - M_RightBorder, c)
        Next
        G.DrawLine(PenMajor, M_LeftBorder, Track_Max, Me.Width - M_RightBorder, Track_Max)
        G.DrawLine(PenMajor, M_LeftBorder, Track_Min, Me.Width - M_RightBorder, Track_Min)
        Dim L As Integer = Center - (M_KnobWidth / 2)
        G.FillRectangle(ClearBrush, L, Track_Max - 3, M_KnobWidth, Track_Len + 6)
        'Draw Track
        Dim TrackPen As New Pen(M_TrackColor, 1)
        'G.DrawLine(Pens.Black, Center, 0, Center, Me.Height)
        Dim TrackWidth As Integer = 2
        Dim TrackRect As New Rectangle(Center - TrackWidth, Track_Max, (TrackWidth * 2), Track_Len)
        G.FillRectangle(Brushes.Black, TrackRect)
        Me.BackgroundImage = BackgroundPic
    End Sub
    Private Sub DrawKnob(ByVal G As Graphics)
        Dim KnobVal As Integer
        If MouseVal = 0 Then
            KnobVal = Track_Min
        Else
            KnobVal = MouseVal
        End If
        'Draw Knob Line
        Dim LP As New Pen(M_ExtensionLineColor, M_ExtensionLineWidth)
        If M_ShowKnobExtenderLine Then G.DrawLine(LP, M_LeftBorder, KnobVal, Me.Width - M_RightBorder, KnobVal)

        'Draw Knob
        Dim x As Integer = Center - (Knob.Width / 2)
        Dim y As Integer = KnobVal - (Knob.Height / 2)
        'G.DrawRectangle(Pens.Blue, x, y, 20, 10)
        G.DrawImage(Knob, x, y)
    End Sub
    <System.ComponentModel.Category("Knob")> Public Property KnobWidth() As Integer
        Get
            Return M_KnobWidth
        End Get
        Set(ByVal value As Integer)
            If value > 15 Then
                M_KnobWidth = value
                Knob = BackPic.CreateScaledPic(M_KnobWidth, M_Knobheight, M_KnobColor, 0.5)
                Refresh()
            End If
        End Set
    End Property
    <System.ComponentModel.Category("Knob")> Public Property KnobHeight() As Integer
        Get
            Return M_Knobheight
        End Get
        Set(ByVal value As Integer)
            If value > 11 Then
                M_Knobheight = value
                Knob = BackPic.CreateScaledPic(M_KnobWidth, M_Knobheight, M_KnobColor, 0.5)
                Refresh()
            End If
        End Set
    End Property
    <System.ComponentModel.Category("Knob")> Public Property KnobColor() As Color
        Get
            Return M_KnobColor
        End Get
        Set(ByVal value As Color)
            M_KnobColor = value
            Refresh()
        End Set
    End Property
    <System.ComponentModel.Category("Knob")> Public Property ShowExtensionLine() As Boolean
        Get
            Return M_ShowKnobExtenderLine
        End Get
        Set(ByVal value As Boolean)
            M_ShowKnobExtenderLine = value
            Refresh()
        End Set
    End Property
    <System.ComponentModel.Category("Knob")> Public Property ExtensionLineColor() As Color
        Get
            Return M_ExtensionLineColor
        End Get
        Set(ByVal value As Color)
            M_ExtensionLineColor = value
            Refresh()
        End Set
    End Property
    <System.ComponentModel.Category("Knob")> Public Property ExtensionLineWidth() As Integer
        Get
            Return M_ExtensionLineWidth
        End Get
        Set(ByVal value As Integer)
            M_ExtensionLineWidth = value
            Refresh()
        End Set
    End Property
    <System.ComponentModel.Category("Background")> Public Property BackgroundType() As BackGroundTypeEnum
        Get
            Return M_BackgroundType
        End Get
        Set(ByVal value As BackGroundTypeEnum)
            M_BackgroundType = value
            Refresh()
        End Set
    End Property
    '<System.ComponentModel.Category("Background")> <System.ComponentModel.Browsable(False)> Public Overrides Property BackColor() As Color
    '     Get
    '     End Get
    '     Set(ByVal value As Color)
    '     End Set
    ' End Property
    <System.ComponentModel.Category("Background")> Public Property BackgroundColor() As Color
        Get
            Return m_BackGroundColor
        End Get
        Set(ByVal value As Color)
            m_BackGroundColor = value
            Refresh()
        End Set
    End Property
End Class
